Imports System.Data.SqlClient
Imports System.Data
Imports CAPA_Entidad
Imports CAPA_Utilitarios
Public Class DocumentoAlmacenDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function registrarDocumentoAlmacen(ByVal da As DocumentoAlmacen) As Boolean
        Dim con As SqlConnection = conex.obtenerConexion()
        Dim valor As Boolean = False

        Try
            con.Open()

            Dim query As String = "USP_RegistrarDocumentoAlmacen"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", da.CIA)
                .Parameters.AddWithValue("@SUC", da.SUC_Codigo)
                .Parameters.AddWithValue("@TDOC", da.TDOC_Codigo)
                .Parameters.AddWithValue("@DCACOD", da.DCA_Codigo)
                .Parameters.AddWithValue("@EST", da.EST_Codigo)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function actualizarDocumentoAlmacen(ByVal da As DocumentoAlmacen) As Boolean
        Dim con As SqlConnection = conex.obtenerConexion()
        Dim valor As Boolean = False

        Try
            con.Open()

            Dim query As String = "USP_ActualizarDocumentoAlmacen"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", da.CIA)
                .Parameters.AddWithValue("@SUC", da.SUC_Codigo)
                .Parameters.AddWithValue("@TDOC", da.TDOC_Codigo)
                .Parameters.AddWithValue("@DCACOD", da.DCA_Codigo)
                .Parameters.AddWithValue("@EST", da.EST_Codigo)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function obtenerListadoDocumentoAlmacen(ByVal codTipo As String) As DataSet
        Dim ds As New DataSet
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "SELECT * FROM VW_DocumentoAlmacen WHERE TDOC_Codigo = @TDOC"
            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@TDOC", codTipo)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            adapter.Fill(ds)
        Catch ex As Exception
            ds = Nothing
        Finally
            con.Close()
        End Try

        Return ds
    End Function

    Function obtenerDocumentoAlmacenXID(ByVal codCIA As String, ByVal codSUC As String, ByVal codTDOC As String, ByVal codDCA As String) As DocumentoAlmacen
        Dim da As New DocumentoAlmacen("-1", "", "", "", "", "", "")
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "USP_ObtenerDocumentoAlmacenXID"
            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandType = CommandType.StoredProcedure
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDOC", codTDOC)
                .Parameters.AddWithValue("@DCA", codDCA)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            Dim ds As New DataSet
            adapter.Fill(ds)

            If ds.Tables(0).Rows.Count > 0 Then
                da = New DocumentoAlmacen(ds.Tables(0).Rows(0)("CIA").ToString.Trim, ds.Tables(0).Rows(0)("SUC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("TDOC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DCA_Codigo").ToString.Trim, _
                                          ds.Tables(0).Rows(0)("DCA_FechaDocumento").ToString.Trim, ds.Tables(0).Rows(0)("EST_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("DCA_FTermino").ToString.Trim)
            End If
        Catch ex As Exception
            da = Nothing
        Finally
            con.Close()
        End Try

        Return da
    End Function

    '--------------------------------------------------------------------------------

    Function obtenerUltimoCodigoDCA(ByVal tipoDOC As String) As Integer
        Dim numero As Integer = 0
        Dim con As SqlConnection = conex.obtenerConexion()

        Try
            con.Open()

            Dim query As String = "SELECT COUNT(*) + 1 AS Contar " & _
                                  "FROM Documento_Almacen " & _
                                  "WHERE TDOC_Codigo = '" & tipoDOC & "'"

            Dim adapter As SqlDataAdapter = New SqlDataAdapter(query, con)
            Dim ds As New DataSet
            adapter.Fill(ds)

            numero = CInt(ds.Tables(0).Rows(0)("Contar").ToString.Trim)
        Catch ex As Exception
            numero = -1
        Finally
            con.Close()
        End Try

        Return numero
    End Function

End Class
